<?php
    include('../Action/base.php');
    Class ValidationModel extends Model {
        /*
            验证函数
        */
        Public function validation($arr){
            $result = -1;
            foreach ($arr as $k => $v){
                switch ($k){
                    case 'name':
                        if(preg_match ("/^[\x{4e00}-\x{9fa5}]{2,4}+$/u",$arr['name'])==0){
                         $result =  '名字必须为2到4个汉字!';
                        }
                        continue;

                    case 'date':
                        $date = explode("-",$arr['date']);
                        /*
                            checkdate不太好使!!!!!
                         */
                        $res = checkdate($date[1],$date[2],$date[0]);
                        if($res==false){
                            $result = '出生日期不合法';
                        }
                        continue;

                    case 'num':
                        if(preg_match ("/^[\d]{12}+$/u",$arr['num'])==0){
                            $result =  '学号必须为12位数字';
                        }
                        continue;

                    case 'place':
                        if(preg_match ("/^[\x{4e00}-\x{9fa5}]{1,}+$/u",$arr['place'])==0){
                            $result =  '籍贯必须为汉字';
                        }
                        continue;

                    case 'comeDate':
                        $date = explode("-",$arr['comeDate']);
                        $res = checkdate($date[1],$date[2],$date[0]);
                        if($res==false){
                            $result = '入学日期不合法';
                        }
                        continue;

                    case 'grade1':
                    case 'grade2':
                    case 'grade3':
                    case 'addGrade':
                    case 'subGrade':
                        if(preg_match ("/^[\d]{1,3}+$/u",$arr['grade1'])==0&&$arr['grade1']!=null
                            ||preg_match ("/^[\d]{1,3}+$/u",$arr['grade2'])==0&&$arr['grade2']!=null
                            ||preg_match ("/^[\d]{1,3}+$/u",$arr['grade3'])==0&&$arr['grade3']!=null
                            ||preg_match ("/^[\d]{1,3}+$/u",$arr['addGrade'])==0&&$arr['addGrade']!=null
                            ||preg_match ("/^[\d]{1,3}+$/u",$arr['subGrade'])==0&&$arr['subGrade']!=null){
                            $result =  '学号必须为1~3位数字';
                        }
                        continue;

                    case 'reason1':
                    case 'reason2':
                    case 'reason3':
                    case 'addReason':
                    case 'subReason':
                        if(preg_match ("/^[\x{4e00}-\x{9fa5}]{1,}+$/u",$arr['reason1'])==0&&$arr['reason1']!=null
                            ||preg_match ("/^[\x{4e00}-\x{9fa5}]{1,}+$/u",$arr['reason2'])==0&&$arr['reason2']!=null
                            ||preg_match ("/^[\x{4e00}-\x{9fa5}]{1,}+$/u",$arr['reason3'])==0&&$arr['reason3']!=null
                            ||preg_match ("/^[\x{4e00}-\x{9fa5}]{1,}+$/u",$arr['addReason'])==0&&$arr['addReason']!=null
                            ||preg_match ("/^[\x{4e00}-\x{9fa5}]{1,}+$/u",$arr['subReason'])==0&&$arr['subReason']!=null){
                            $result =  '原因必须为汉字';
                        }
                        continue;
                }
            }
            return $result;
        }

        /*
            查重函数
         */
        Public function Duplicate($tableName,$fieldName,$fieldValue){
            $where["$fieldName"] = $fieldValue;
            $tab = M("$tableName")->where($where)->select();
            if ($tab==null){
                return false;   //虽说返回的是false,但用null判断也成立
            }else{
                return $tab;
            }
        }
    }
?>